layui.define(['table', 'jquery', 'element','layer','table'], function (exports) {
    "use strict";

    var MOD_NAME = 'operate',
        $ = layui.jquery,
        layer = layui.layer;
     var  table = layui.table;


    var operate = new function () {

        this.arrayToStr = function(data){

            var ids ='';
            if(data.length>0){
                for(var i=0;i<data.length;i++){
                    ids+=data[i].id + ',';
                }
                ids = ids.substr(0,ids.length-1);
            }

            return ids;

        }

        this.goBack = function(){
            window.history.go(-1);
        }

        /**
         * 打开弹层
         * @param title 标题
         * @param url 接口
         * @param size 自定义大小
         * @param callback 回调函数
         * */
        this.open = function(title,url,size){
            layer.open({
                type: 2,
                title: title,
                shadeClose: true,
                shade: false,
                area: [size.width,size.height],
                content: url
            });
        }

        /**
         * 最大化打开弹层
         * @param title 标题
         * @param url 接口
         * */
        this.openFull = function(title,url){
            layer.open({
                type: 2,
                title: title,
                shadeClose: true,
                shade: false,
                area: ['100%', '100%'],
                content: url
            });
        }

        /**
         * 根据 Keys 批量删除
         * @param url 批量接口
         * @param ids 要删除的数据
         * @param table 刷新表格
         * */
        this.removeByIds = function(url,ids,tableName){

            if(ids==='' || ids == null){
                operate.warming("未选中数据");
                return false;
            }

            var loading =  layer.load();

            operate.delete(url+'/'+ids,null,function(result){
                layer.close(loading);

                if(result.code==200){
                    operate.success(result.msg,function(){
                        table.reload(tableName);
                    })
                }else{
                    operate.failure(result.msg);
                }
            })

        }

        /**
         * 根据 Key 删除数据
         * @param url 接口
         * @param id  删除标识
         * @param obj 行对象
         * */
        this.removeById = function(url,id,tableName){
            layer.confirm('真的要删除嘛', function(index) {
                layer.close(index);
                var loading = layer.load();

                operate.delete(url+'/'+id,null,function(result){
                    layer.close(loading);
                    if(result.code==200){
                        operate.success(result.msg,function(){
                            table.reload(tableName);
                        })
                    }else{
                        operate.failure(result.msg);
                    }
                })


            });
        }

        /**
         * 保存新增数据
         * @param url   保存接口
         * @param param 修改数据
         * @param table 刷新表格
         * */
        this.save = function(url,param,tableName){
            var loading = layer.load();
            operate.post(url,param,function(result){
                layer.close(loading);
                if(result.code == 200){
                    operate.success(result.msg,function(){
                        //关闭当前窗体
                        parent.layui.table.reload(tableName);
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    })
                }else{
                    operate.failure(result.msg);
                }
            })


        }

        /**
         * 保存修改数据
         * @param url   修改接口
         * @param param 修改数据
         * @param table 刷新表格
         * */
        this.edit = function(url,param,tableName){
            var loading  = layer.load();
            operate.put(url,param,function(result){
                layer.close(loading);
                if(result.code==200){
                    operate.success(result.msg,function(){
                        //关闭当前窗体
                        parent.layui.table.reload(tableName);
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    })
                }else{
                    operate.failure(result.msg);
                }
            })
        }

        this.success = function(msg,callback){
            layer.msg(msg,{icon:1,time:1200},callback);
        }

        this.failure = function(msg,callback){
            layer.msg(msg,{icon:2,time:1200},callback);
        }

        this.warming = function(msg,callback){
            layer.msg(msg,{icon:3,time:1200},callback);
        }

        /**
         * HTTP GET 请求
         * @param url 接口
         * @param param 携带参数
         * @param success 回调函数
         * */
        this.get = function(url,param,success){
            $.ajax({
                url:url,
                data:param,
                dataType:'json',
                type:'get',
                success:function(result){
                    success(result);
                },
                error:function(){
                    operate.failure("system failure code 520");
                }
            })
        }

        /**
         * HTTP POST 请求
         * @param url 接口
         * @param param 携带参数
         * @param success 回调函数
         * */
        this.post = function(url,param,success){
            $.ajax({
                url:url,
                data:JSON.stringify(param),
                contentType: 'application/json; charset=UTF-8',
                dataType:'json',
                type:'post',
                success:function(result){
                    success(result);
                },
                error:function(){
                    operate.failure("system failure code 520");
                }
            })
        }

        /**
         * HTTP PUT 请求
         * @param url 接口
         * @param param 携带参数
         * @param success 回调函数
         * */
        this.put = function(url,param,success){
            $.ajax({
                url:url,
                data:JSON.stringify(param),
                contentType: 'application/json; charset=UTF-8',
                dataType:'json',
                type:'put',
                success:function(result){
                    success(result);
                },
                error:function(){
                    operate.failure("system failure code 520");
                }
            })
        }

        /**
         * HTTP DELETE 请求
         * @param url 接口
         * @param param 携带参数
         * @param success 回调函数
         * */
        this.delete = function(url,param,success){
            $.ajax({
                url:url,
                data:param,
                dataType:'json',
                type:'delete',
                success:function(result){
                    success(result);
                },
                error:function(){
                    operate.failure("system failure code 520");
                }
            })
        }


    };

    exports(MOD_NAME,operate);
})